import React from "react";

export default class SetStateDemo extends React.Component{
    constructor(){
        super();

        this.state={
            count:0
        }
    }
    render(){
        return(
            <div>
                setState同步还是异步
                <p>{this.state.count}</p>
                <button onClick={this.increment1}>1更新count</button>
                <button onClick={this.increment2.bind(this)}>2更新count</button>
            </div>
        )
    }

    increment1=()=>{
        this.setState({
            count:this.state.count+1
        },()=>{
            console.log("b:count:"+this.state.count);
        })
        console.log("a:count:"+this.state.count);
    }

   async increment2(){
       await this.setStateAsync({count:this.state.count+1})
       console.log(this.state.count)
   }

    //Promise Async await es6的新特性參考
    setStateAsync(state){
        return new Promise((resolve)=>{
            this.setState(state,resolve)
        })
    }
}